package com.example.sample.activity;

import android.app.Activity;
import android.app.LoaderManager;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.example.app.BaseActivity;
import com.example.sample.R;

import net.liteheaven.xblib.common.ui.ToastUtil;

import java.io.File;
import java.lang.reflect.Field;

import dalvik.system.DexClassLoader;

/**
 * Created by liangy on 2016/8/12.
 */
public class ClassLoaderActivity extends BaseActivity {

    private View button_load_plugin;

    public class MyDexClassLoader extends DexClassLoader{
        protected MyDexClassLoader(String dexPath, String optimizedDirectory,
                                   String libraryPath, ClassLoader parent) {
            super(dexPath, optimizedDirectory, libraryPath, parent);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_class_loader);
        button_load_plugin = findViewById(R.id.button_load_plugin);
        button_load_plugin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean result = loadPlugin();
                ToastUtil.show(ClassLoaderActivity.this, "加载"+result);
            }
        });
    }

    private boolean loadPlugin(){
        boolean result = false;

        String apkPath = "/sdcard/Download/app-debug.apk";
        File file = new File(apkPath);

        String dexOutputPath = getDir("plugin", Context.MODE_PRIVATE).getAbsolutePath();
        final String libOutputPath = getDir("plugin_lib", Context.MODE_PRIVATE).getAbsolutePath();
        ClassLoader parentLoader = this.getClass().getClassLoader();
        //ClassLoader parentLoader = ClassLoader.getSystemClassLoader();
        MyDexClassLoader dexClassLoader = new MyDexClassLoader(file.getAbsolutePath(), dexOutputPath, libOutputPath, parentLoader);

        /*Class<?> clazz = null;
        String className = "net.liteheaven.nullproject.MainActivity";
        try {
            clazz = dexClassLoader.loadClass(className);
            result = true;
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            result = false;
        }

        Activity activity = null;
        try {
            activity = (Activity) clazz.newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        Class<?> [] argTypes = new Class<?> [] {
                Bundle.class
        };
        Object [] argObjs = new Object[]{
                new Bundle()
        };
        ReflectUtil.invokeMethod(activity, "onCreate", argTypes, argObjs);*/

        Object ret = null;
        try {
            Field f = Activity.class.getDeclaredField("mFragments");
            f.setAccessible(true);
            Object mFragments = f.get(this);
            f.setAccessible(false);
        } catch (Exception e) {
            e.printStackTrace();
            Throwable cause = e.getCause();
        }

        Field[] field, field2;
        Field f;
        try {
            field2 = Activity.class.getFields();
            field = Activity.class.getDeclaredFields();
            f = Activity.class.getDeclaredField("mLoaderManager");
            field = Context.class.getDeclaredFields();
        } catch (Exception e) {
            e.printStackTrace();
        }

        LoaderManager lm = getLoaderManager();


        return result;
    }
}
